<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

/**
 * This is the default homepage for the site
 * @author X.Z.LIANG
 *
 */
class Home extends CI_Controller {

 
	public function index()
	{
		
		$personal = set_global_cookie();
		 
		if($personal['user_sign_in'] == false){
			// not sign in
			$main    =  $this->smarty->view('main/front.tpl',null,true);
			$this->output->set_output(make_output($this->smarty,$main));
		}
		else{
			$this->load->model(array('checkin_model','photo_model','message_model','friend_model','user_model'));
			
			$data = array('username'         =>  $personal['username'],
						  'realname'         =>  $personal['realname'],
						  'profile_photo'    =>  get_profile_photo() ,
						  'photo_amount'	 =>  $this->photo_model->count_photo($personal['username']),
						  'message_amount'   =>  $this->message_model->message_count($personal['username']),
						  'friend_amount'    =>  $this->friend_model->friend_count($personal['username']),
						  'notice_amount'	 =>  $this->friend_model->unanswer_req_count($personal['username']),
						  'checkin_amount'	 =>  $this->checkin_model->checkin_count($personal['username'])
	 		);
			$main    =  $this->smarty->view('main.tpl',$data,true);
			$this->output->set_output(make_output($this->smarty,$main));
		}
	}
	
	
	
	function wall()
	{
		
		$this->load->model(array('photo_model','message_model','checkin_model','user_model'));
		
		/** photo **/
		$access_user= get_cookie("username");
		$start = 0;$end = 20;
		if(isset($_POST['start'])){
			$start = $_POST['start'];
		}
		if(isset($_POST['end'])){
			$end = $_POST['end'];
		}
		
		if(empty($access_user) )
		{
			return false;
		}
		$data['photos'] = $this->photo_model->access_photo($access_user,$start,$end);
		
		if($data['photos']){
			foreach($data['photos'] as $k =>&$v){
				$v['pic'] = get_profile_photo_by_id(ord(substr($v['u_username'],0,1))%2);
			}
		}
		
		$data['friend_message'] = $this->message_model->friend_message($access_user,$start,$end);
		if($data['friend_message']){
		
			foreach($data['friend_message'] as $k =>&$v){
				$v['pic'] = get_profile_photo_by_id(ord(substr($v['user_from'],0,1))%2);
			}
		}
		
		$data['user_message'] = $this->message_model->user_message($access_user,$start,$end);
		if($data['user_message']){
			foreach($data['user_message'] as $k =>&$v){
				$v['pic'] = get_profile_photo_by_id(ord(substr($v['user_from'],0,1))%2);
			}
		}
		
		$data['checkin_message'] = $this->checkin_model->latest_checkin($start,$end);
		if($data['checkin_message']){
			foreach($data['checkin_message'] as $k =>&$v){
				$v['pic'] = get_profile_photo_by_id(ord(substr($v['u_username'],0,1))%2); 
			}
		}
		
		$data['my_circle'] = array();
		$friends_type= array("train buddies","college friends","family");
		foreach($friends_type as $i){
			$data['my_circle'][$i]  = $this->user_model->circle_users($access_user,$start,$end,$i);
			if($data['my_circle'][$i]){
				foreach($data['my_circle'][$i] as $k =>&$v){
					$v['pic'] = get_profile_photo_by_id(ord(substr($v['username'],0,1))%2);
				}
			}
		}
		$output = $this->smarty->view('main/wall.tpl',$data,true);
		$this->output->set_output($output);
		
	}
	
	
	
	function search()
	{
	
		$this->load->model(array('photo_model','message_model'));
		$keyword = '';
		/** photo **/
		$access_user= get_cookie("username");
		if(isset($_POST['keyword'])){
			$keyword = $_POST['keyword'];
		}
		$start = 0;$end = 20;
	
		if(empty($access_user) )
		{
			return false;
		}
		
		$data['photos'] = $this->photo_model->keyword_fre_photo($access_user,$keyword,$start,$end);
	
		if($data['photos']){
			foreach($data['photos'] as $k =>&$v){
				$v['pic'] = get_profile_photo_by_id(ord(substr($v['u_username'],0,1))%2);
			}
		}
	
		$data['friend_message'] = $this->message_model->friends_all_message($access_user,$keyword,$start,$end);
		if($data['friend_message']){
	
			foreach($data['friend_message'] as $k =>&$v){
				$v['pic'] = get_profile_photo_by_id(ord(substr($v['user_from'],0,1))%2);
			}
		}
	
		 
	
		 
		$output = $this->smarty->view('main/search.tpl',$data,true);
		$this->output->set_output($output);
	
	}
	
	
	function advanced_search()
	{
		$output = $this->smarty->view('main/advanced_search.tpl',null,true);
		$this->output->set_output($output);
	}
	function advanced_search_result()
	{
		$this->load->model(array('search_model'));
		$u_name = get_cookie('username');
		
		$data = array();
		if($_POST['type'] == 'user')
		{
			$data['user'] = $this->search_model->compound_find_user($u_name,$_POST['lat'] ,$_POST['long'] ,$_POST['period'] ,$_POST['distant']);
		 
		}else 
		{
			$data['photo'] = $this->search_model->compound_find_photo($_POST['lat'] ,$_POST['long'] ,$_POST['period'] ,$_POST['distant']);
			
		}
		
		$output = $this->smarty->view('main/search.tpl',$data,true);
		$this->output->set_output($output);
	}
	
}










/* End of file home.php */
/* Location: ./application/controllers/home.php */